package com.zqs.compareface.server.imageProcess;

import com.zqs.compareface.utils.ConnectionLocal;
import com.zqs.compareface.utils.impl.ImageToBase64Util;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameConverter;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

import java.awt.image.BufferedImage;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Program: FaceCompare
 * @Description:
 * @Author: zqs
 * @Create： 2021-08-11-22:03
 */
public class SaveOrConvertFaceImage {
    private int i;
    private Mat mat;
    public static boolean contrast = false;
    public static ConcurrentHashMap<Mat,String> hashMap = new ConcurrentHashMap<>();

    public SaveOrConvertFaceImage(int i, Mat mat) {
        this.i = i;
        this.mat = mat;
    }
    public String saveFaceImage(Mat mat){
        String imagePath = null;
        if(contrast) {
            imagePath = ConnectionLocal.STORE_FILE_PATH + i + ".png";
            Imgcodecs.imwrite(imagePath, mat);
        }
        return imagePath;
    }

    public String convertToBase64(Mat mat){
        OpenCVFrameConverter.ToOrgOpenCvCoreMat converter = new OpenCVFrameConverter.ToOrgOpenCvCoreMat();
        Frame frame = converter.convert(mat);
        BufferedImage bufferedImage = new Java2DFrameConverter().getBufferedImage(frame);
        return ImageToBase64Util.convertBase64(bufferedImage);
    }
    public void setHashMap(){
        hashMap.put(mat,convertToBase64(mat));
    }
}
